package com.zhoug.viewpermission;

import android.os.Build;


import com.zhoug.logging.Logger;

import java.lang.annotation.Annotation;

import androidx.annotation.NonNull;

/**
 * 注解 {@link BindMenu}解析器
 * @Author: zhoug
 * @Date: 2024-12-24
 * @Description:
 */
public class BindMenuParser {
    private static final String TAG = ">>>BindMenuParser";
    private static final boolean DEBUG = true;
    private final Object mTarget;

    public BindMenuParser(Object target) {
        mTarget = target;
    }

    public BindMenu parse() {
        if (mTarget == null) {
            Logger.e(TAG, "parse:mTarget is null");
        }
        return getDeclaredAnnotation(BindMenu.class);
    }


    /**
     * @noinspection unchecked
     */
    private <A extends Annotation> A getDeclaredAnnotation(@NonNull Class<A> annotationClass) {
        if (mTarget == null) return null;
        Class<?> mTargetClass = mTarget.getClass();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return mTargetClass.getDeclaredAnnotation(annotationClass);
        } else {
            Annotation[] annotations = mTargetClass.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation.annotationType() == annotationClass) {
                    return (A) annotation;
                }
            }
        }
        return null;
    }


}
